import requests
import urllib.request
import urllib.parse


def smscode(mobile, code):
    url = "https://gyytz.market.alicloudapi.com/sms/smsSend"

    appcode = '6500617db99a428284511d9bfc2b9288'

    smsSignId = "dccae958f42c42458d4a5a85dd231405"
    # mobile = ""
    templateId = "c56b991b296d42c7b0eb713b3405ed8b"
    param = f'**code**:%s,**minute**:5' % code

    # smsSignId（短信前缀）和templateId（短信模板），可登录国阳云控制台自助申请。参考文档：http://help.guoyangyun.com/Problem/Qm.html

    data = {"mobile": mobile, "smsSignId": smsSignId, "templateId": templateId, "param": param}

    headers = {"Content-Type": "application/x-www-form-urlencoded", "Authorization": "APPCODE "+appcode}

    response = requests.post(url, headers=headers, params=data)
    return response


import urllib, sys
import ssl


def phone_code(number, code):
    host = 'https://wwsms.market.alicloudapi.com'
    path = '/send_sms'
    method = 'POST'
    appcode = '6500617db99a428284511d9bfc2b9288'
    querys = ''
    bodys = {}
    url = host + path

    bodys['content'] = f'code:{code}'  # 使用f-string替换之前的格式化字符串
    bodys['template_id'] = 'TPL_0000'
    bodys['phone_number'] = number
    post_data = urllib.parse.urlencode(bodys)
    post_data_encoded = urllib.parse.urlencode(bodys).encode('utf-8')
    request = urllib.request.Request(url, post_data_encoded)
    request.add_header('Authorization', 'APPCODE ' + appcode)
    # //根据API的要求，定义相对应的Content-Type
    request.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    response = urllib.request.urlopen(request, context=ctx)
    content = response.read()
    if (content):
        print(content)
    return content
